package com.liu.nb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.nb.entity.RolePage;
import com.liu.nb.service.IRolePageService;
import com.liu.nb.util.Result;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 角色和权限中间表 前端控制器
 * </p>
 *
 * @author zk
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/nb/role-page")
public class RolePageController {
    @Resource
    private IRolePageService rolePageService;
    @PostMapping("/add/{roleId}/{pagesId}")
    //添加角色权限，一个角色对应多个权限
    public Result add(@PathVariable Integer roleId,@PathVariable Integer[] pagesId){
        int i=0;
        boolean rem = rolePageService.remove(new QueryWrapper<RolePage>().eq("role_id", roleId));

        do {
            RolePage rolePage=new RolePage();
            rolePage.setRoleId(roleId);
            rolePage.setPageId(pagesId[i]);
            if(rolePageService.save(rolePage)==false){
                return Result.success("no");
            }
            i++;
        } while(i<pagesId.length);

        return Result.success("ok");
    }

}
